define(function(require, exports){
	var pubjs = require('pubjs');
	var util	= require('util');
	var grid  = require('@base/highGrid');
	var $ = require('jquery');
	var common = require('modules/common');

	var auditUtil  = require('../auditUtil');

	var HighGrid = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
						url: 'creativeAudit/preAuditList',
						getAptitudeInfoUrl:'creativeAudit/getAptitudeInfo',
						param: {
							'IsDeleted': false,
							'idType':config.idType,
							status:-2 //默认筛选状态：审核中
						},
						cols: [
							{name:"_audit",text:'操作',render:'renderOp',width:60,align:'center'},
							{type:"select"},
							{name:'_id',text:"创意Id",width:80,align:'center',render: 'renderId',sort:true},
							{name:'Name',text:"创意名称",align:'left',render:'renderCreativeName',width:300}
						],
						metrics:[
							{name:'AuditList',align:'left',text:"预审状态",render:'renderStatus',desc:LANG('蓝：审核中;绿：通过;拒绝：红')},
							{name:'SweetyId',text:"创意包ID",width:60,align:'center',render: 'renderId'},
							{name:'SweetyName',text:"创意包名称",render: 'renderSweetyName',align:'left'},
							{name:'CreateUserId',text:"客户ID",width:60,align:'center',render: 'renderId'},
							{name:'CreateUserName',align:'left',text:"客户账号", render: 'renderName',width:280},
							{name:'CreateTime',align:'left',text:"上传时间",render:'renderTime'}
						],
						reqType:'websocket',
						eventDataLoad: true,
						hasBatch: true,
						hasTab: false,
						hasRefresh: true,
						hasExport: false,
						hasSelect: false,
						hasSwitch: false,
						hasAmount: false,
						auto_load: true,
						key:config.idType? 'AuditId':'_id',
						pager: {'sizeTypes':[10,20,50,100,200,300,400,500]},
						gridName: 'creativePreAudit',
						listRowHoverKey: 'data-index'
					}
			);
			this.Super('init', arguments);
			//继承展示资质的方法
			Object.assign(this,auditUtil.showAptitudeData);
		},
		renderId: function(i,val,data,con){
			var html = $('<div class="text-truncate"/>').text(val);
			// 双击出现复制按钮
			this.uiBind(html, 'dblclick', function(evt, elm){
				pubjs.clipboard(html);
			});
			return html;
		},
		renderOp:function(i,val,data,con){
			var $btn = $("<button class='auditBtn btn'>审核</button>");
			$btn.data(data);
			this.uiBind($btn,'click','eventAuditCreative');
			return $btn;
		},
		renderName:function(i,val,data,con){
			var html = $('<label title="'+ val + '" class="text-truncate dib">').text(val);
			// 双击出现复制按钮
			this.uiBind(html, 'dblclick', function(evt, elm){
				pubjs.clipboard(html);
			});
			return html;
		},
		renderSweetyName:function(i,val,data,con){
			var html = $('<a target="_blank" href="' + data.SweetyThumb + '" "title="'+ val + '" class="text-truncate dib">').text(val);
			return html;
		},
		renderCreativeName:function(i,val,data,con){
			var $previewBtn = $("<a class='text-truncate preview db' title='"+ val +"' data-origin='" + data.Thumb + "'>" + (val?val:'') + "</a>");
			$previewBtn.data(data);
			if (data.Type == 3 || data.youku_html){
				//外链类型打开新
				$previewBtn.attr({'href':data.youku_html || data.Path,target:'blank'});
			} else {
				this.uiBind($previewBtn,'click imageLoad imageError','_showPreviewImage');
			}
			return $previewBtn.width(con.width-20);
		},
		renderStatus:function(index,val,data,con){
			if (!data.AuditList) return "无";

			var color_map = {
				'1': 'green'
				,'-1': 'red'
			};

			var html = '';

			if(!data.AuditList){
				data.AuditList = [];
			}
			var infoSplitLength = 30;
			for(i = 0 ;i < data.AuditList.length; i++){
				var audit = data.AuditList[i];
				var whiskyUrl = pubjs.config('materialPreview/whisky').replace('{id}', audit.WhiskyId);
				var color = color_map[audit.PreStatus] || 'blue';

				var aptitudeHtml = '';
				if(audit.Aptitude){
					aptitudeHtml = `，<a class="auditAptitudeInfo" data-index="${index}" data-creativeId="${data._id}" style="color:${color}"
 								data-aptitudeId="${audit.Aptitude.AptitudeId}">资质ID:${audit.Aptitude.AptitudeId}</a>`;
				}

				var $status = `<font color=${color}
									<b>${audit.ExchangeName? audit.ExchangeName:'未知渠道:'+ audit.ExchangeId}
										(<a style="color:${color}" target="_blank" href="${whiskyUrl}">
											WhiskyId:${audit.WhiskyId}
											${audit.AdxDealId ? ',AdxDealId:' + audit.AdxDealId: ''}
										</a>
										${aptitudeHtml}
										)
									</b>
									<span>${audit.PreInfo ? (audit.PreInfo.length >infoSplitLength ?audit.PreInfo.substr(0,infoSplitLength) + '......' :audit.PreInfo):""}</span>
									<a class="auditInfoDetail ml10" style="display:${audit.PreInfo && audit.PreInfo.length > infoSplitLength ?'inline':'none'};" data-detail="${audit.PreInfo||''}">详情</a>
								</font>
								<br/>
				`;

				html += $status;
			}

			var $html = $(html);
			this.uiBind($html.find('a.auditInfoDetail'),'click','showAuditInfo');
			this.uiBind($html.find('a.auditAptitudeInfo'),'click','showAptitudeInfo');
			return $html ;
		}
		,renderTime: function(i, val) {
			return val ? util.date("Y-m-d H:i:s", val) : "";
		},
		eventAuditCreative:function(e){
			var data = $(e.target).data();
			this.fire('openAuditPopwin',data);
		},
		showAuditInfo:function (ev) {
			var el = $(ev.target);
			var html = $(el).data('detail');
			auditUtil.showInfo.call(this,'previewInfo',ev,html);
		},
		showAptitudeInfo:function (ev) {
			var el = $(ev.target);
			this.$showAptitudeInfoEventTarget = ev;
			var creativeId = $(el).data('creativeid');
			var aptitudeId = $(el).data('aptitudeid');
			this.$showAptitudeRowIndex = +$(el).data('index');
			this.getAptitudeData(creativeId,aptitudeId);
		},
		onRefreshManual: function(){
			// 手动刷新，清除选中的创意
			this.setSelectRowIds([]);
			this.load();
			this.clearAptitudeDataCache();
			return false;
		},
		_showPreviewImage:function(ev){
			auditUtil.showPreviewImage.call(this,ev);
		}
	});

	exports.base = HighGrid;

});